/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util.log;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

//create a rolling log files
//http://kodejava.org/how-do-i-create-a-rolling-log-files/

/**
 *
 * @author Rdelacruz
 */
public class LoggerUtil {
    
    //http://stackoverflow.com/questions/2533227/how-can-i-disable-the-default-console-handler-while-using-the-java-logging-api
    public static void removerHandlerConsola(Logger logger) {
        logger.setUseParentHandlers(false);
    }    
    
    //http://stackoverflow.com/questions/2533227/how-can-i-disable-the-default-console-handler-while-using-the-java-logging-api
    public static void removerHandlersConsolaAntesDeAgregarHandlersPropios() {
        Logger globalLogger = Logger.getLogger("global");
        Handler[] handlers = globalLogger.getHandlers();
        for(Handler handler : handlers) {
            globalLogger.removeHandler(handler);
        }
    }
    
    public static Logger getLoggerSimple(String nombreLogger, File file) throws IOException {        
        // write logs in text file when using java.util.logging.Logger
        //http://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger
              
        Logger logger = Logger.getLogger(nombreLogger); 
        
        //FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log");          
        //FileHandler fh = new FileHandler(file.getAbsolutePath());
        FileHandler fh = new FileHandler(file.getPath());
        SimpleFormatter formatter = new SimpleFormatter();  
        fh.setFormatter(formatter);  
        logger.addHandler(fh);

        //logger.info("Mi");
        return logger;
    }
}
